//
//  SchoolTableView.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "SchoolTableView.h"
#import "SchoolTableViewCell.h"
#import "SchoolModel.h"
#import "BannerModel.h"
#import "BOSS_SOOC-swift.h"
#import "BannerViewController.h"

@interface SchoolTableView()<UITableViewDelegate, UITableViewDataSource,DSBanderDelegate>

@property (nonatomic,strong) DSNewBander *DSbanner;


@end


@implementation SchoolTableView

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    if (self = [super initWithFrame:frame style:style]) {
        
        self.delegate = self;
        
        self.dataSource = self;
        
        self.rowHeight = (SIZEWIDTH/2-68*Bili)*277/486+78*Bili;
        
        [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        /**
         *  cell
         */
        [self registerClass:[SchoolTableViewCell class] forCellReuseIdentifier:@"SchoolTableViewCell"];
        
        /**
         *  轮播图
         */
        _DSbanner= [[DSNewBander alloc]initWithFrame:CGRectMake(0, 0, SIZEWIDTH, 570*0.375*SIZEWIDTH/414)];
        
        _DSbanner.delegation = self;
        
        self.tableHeaderView = _DSbanner;
        /**
         *  刷新加载
         */
        [self setupRefresh];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopRefresh) name:@"isRefresh" object:nil];
        
    }
    
    return self;
}

- (void)stopRefresh{
    
    [self.mj_footer endRefreshing];
    [self.mj_header endRefreshing];
}

- (void)setSourceArr:(NSArray *)sourceArr{
    
    _sourceArr = sourceArr;
    
    [self reloadData];
}

- (void)setBannerArr:(NSArray *)bannerArr{

    _bannerArr = bannerArr;
    
    [_DSbanner loadData:_bannerArr];
    
    [self reloadData];
}

-(void)clickImage:(BannerModel *)data{
    

    if ([self.myDelegate respondsToSelector:@selector(bannerSelected:)]) {
        
        [self.myDelegate bannerSelected:data];
    }
    
}

/**
 *  刷新加载
 */
- (void)setupRefresh{

    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_header beginRefreshing];
        
        _myBlock(YES);
        
    }];
    header.stateLabel.textColor = COLOR_TITLE_999999;
    header.lastUpdatedTimeLabel.textColor = COLOR_TITLE_999999;
    self.mj_header = header;
    
    
    MJRefreshAutoNormalFooter * foot = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_footer beginRefreshing];
        
        _myBlock(NO);
        
    }];
    foot.stateLabel.textColor = COLOR_TITLE_999999;
    self.mj_footer = foot;

}



- (NSInteger)numberOfRowsInSection:(NSInteger)section{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  self.sourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SchoolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SchoolTableViewCell"];
    cell.myModel = _sourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.001;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([self.myDelegate respondsToSelector:@selector(schoolTableViewSelected:resultString:Row:)]) {
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        [self.myDelegate schoolTableViewSelected:self resultString:self.sourceArr[indexPath.row] Row:(int)indexPath.row];
    }
}


@end
